package kr.co.cherryt.listView.item;

import kr.co.cherryt.common.ImageCacheManager;
import kr.co.cherryt.common.Utility;
import kr.co.cherryt.parser.xml.XmlManager;
import kr.co.cherryt.parser.xml.XmlParserCore;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class BaseItemLayout extends LinearLayout {
	
	private final static int LEFT_STYLE_IMAGE = 		  0x10000000;
	private final static int CENTER_STYLE_TEXT = 	 	  0x00100000;
	private final static int RIGHT_STYLE_INDICATOR =  	  0x00000010;
	
	public static int CELL_STYLE_TEXT 			= CENTER_STYLE_TEXT;
	public static int CELL_STYLE_IMAGE_TEXT 	= LEFT_STYLE_IMAGE | CENTER_STYLE_TEXT;
	public static int CELL_STYLE_BASIC 			= LEFT_STYLE_IMAGE | CENTER_STYLE_TEXT | RIGHT_STYLE_INDICATOR;
	
	private int m_nGroupStyle = CELL_STYLE_BASIC;
	
	public final static int CTRL_ID_THUMB_IMAGE =  0x00020000;
	public final static int CTRL_ID_TITLE_TEXT =   0x00021000;
	public final static int CTRL_ID_DECO_IMAGE =   0x00022000;
	public final static int CTRL_ID_ITEM_LAYOUT =  0x00030000;
	public final static int CTRL_ID_ITEM_LAYOUT_HEADER =  0x00040000;
	public final static int CTRL_ID_ITEM_TEXT_HEADER =  0x00050000;
	public final static int CTRL_ID_CUSTOM_BASE =  0x00060000;
	public final static int CTRL_ID_RECO_LAYOUT =  0x00070000;

	public final static int PADDING_DEFUALT = 5;
	public final static int PADDING_10 = 10;
	
	// 좌측, 우측, 중앙과 매핑되는 View
	protected View m_leftView;
	protected View m_centerView;
	protected View m_rightView;

	private ImageView m_imgThumbnail;
	private TextView m_textTitle;
	private ImageView m_imgDeco;
	
	public int m_nUniqueIdx;
	
	protected int m_defStyle = -1;
	protected int m_position = -1;
	protected boolean m_writeMode = false;
	protected int m_nNewsMode = -1;
	
	protected OnClickListener m_parentClickListener;
	
	public boolean m_bCustomCenterLayouParam = false;
	
	protected boolean m_shareYn = false;
	
	private ImageCacheManager m_cacheManager;
	
	private boolean m_bHeader = false;

	
	public BaseItemLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public BaseItemLayout(Context context) {
		super(context);
		init(context);
	}
	
	public BaseItemLayout(Context context, OnClickListener l) {
		super(context);
		m_parentClickListener = l;		
		init(context);
	}
	
	public BaseItemLayout(Context context, OnClickListener l, boolean bHeader) {
		super(context);
		m_parentClickListener = l;
		m_bHeader = bHeader;
		init(context);
	}
	
	public BaseItemLayout(Context context, int defStyle) {
		super(context);
		m_defStyle = defStyle;
		init(context);
	}
	
	public BaseItemLayout(Context context, OnClickListener l, int defStyle) {
		super(context);
		m_parentClickListener = l;
		m_defStyle = defStyle;
		init(context);
	}
	
	private void init(Context context){
		setOrientation(LinearLayout.VERTICAL);
		
		m_cacheManager = ImageCacheManager.getInstance(context);

		LinearLayout itemLayout = new LinearLayout(context);
		itemLayout.setId(CTRL_ID_ITEM_LAYOUT);
		itemLayout.setOrientation(LinearLayout.HORIZONTAL);
		itemLayout.setPadding(10, 10, 0, 10);
		itemLayout.setBackgroundColor(Color.WHITE);
		itemLayout.setGravity(Gravity.CENTER_VERTICAL);
		
		// create
		m_leftView = createLeftView(context, m_nGroupStyle & 0xf0000000);
		m_centerView = createCenterView(context, m_nGroupStyle & 0x00f00000);
		m_rightView = createRightView(context, m_nGroupStyle & 0x000000f0);

		// layout param
		if(m_bCustomCenterLayouParam == false){
			m_centerView.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1.0f));
		}
		
		addView(itemLayout, m_leftView);
		addView(itemLayout, m_centerView);
		addView(itemLayout, m_rightView);
		
		if (m_bHeader) {
			LinearLayout itemLayoutAll = new LinearLayout(context);
			itemLayoutAll.setOrientation(LinearLayout.VERTICAL);
			
			LinearLayout itemLayoutHeader = new LinearLayout(context);
			itemLayoutHeader.setId(CTRL_ID_ITEM_LAYOUT_HEADER);
			itemLayoutHeader.setOrientation(LinearLayout.VERTICAL);
			itemLayoutHeader.setPadding(10, 3, 0, 3);
			itemLayoutHeader.setBackgroundColor(Color.parseColor("#FF9326"));
			itemLayoutHeader.setVisibility(View.GONE);
			
			TextView textHeader = new TextView(context);
			textHeader.setId(CTRL_ID_ITEM_TEXT_HEADER);
			textHeader.setTextColor(Color.parseColor("#333333"));
			textHeader.setText("시청 안한 광고");
			itemLayoutHeader.addView(textHeader);
			
			itemLayoutAll.addView(itemLayoutHeader);
			itemLayoutAll.addView(itemLayout);
			
			addView(itemLayoutAll);
		}
		else {
			addView(itemLayout);
		}
	}
	
	
	// ##############################################
	// ## View 생성 함수
	// ##############################################
	protected View createLeftView(Context context, int nLeftStyle){
		if(nLeftStyle == LEFT_STYLE_IMAGE){
			m_imgThumbnail = createImageView(context, CTRL_ID_THUMB_IMAGE); 
			return m_imgThumbnail;
		}
		return null;
	}

	protected View createCenterView(Context context, int nCeterStyle){
		if(nCeterStyle == CENTER_STYLE_TEXT){
			m_textTitle =  createTextView(context, CTRL_ID_TITLE_TEXT);
			m_textTitle.setPadding(10, 0, 0, 0);
			return m_textTitle;
		}
		return null;
	}
	
	protected View createRightView(Context context, int nRightStyle){
		if(nRightStyle == RIGHT_STYLE_INDICATOR){
			m_imgDeco = createImageView(context, CTRL_ID_DECO_IMAGE);
			
			if(nRightStyle == RIGHT_STYLE_INDICATOR){
				//Utility.setAssetImage(m_imgDeco, LikingAssetManager.ASSET_IMG_INDICATOR);
			}
			return m_imgDeco;
		}
		return null;
	}
	
	
	// ##############################################
	// ## View Getter 함수 (필요하려나~)
	// ##############################################
	final public View getLeftView(){
		return m_leftView;
	}

	final public View getCenterView(){
		return m_centerView;
	}

	final public View getRightView(){
		return m_rightView;
	}
		
	// ##############################################
	// ## Child Widget 생성
	// ##############################################	
	/**
	 * 공통된  ImageView를 생성하기 위한 함수
	 */
	protected ImageView createImageView(Context context, int nId){
		ImageView imgView = new ImageView(context);
		
		LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		param.gravity = Gravity.CENTER_VERTICAL;
		
		imgView.setLayoutParams(param);
		
		if(nId == CTRL_ID_THUMB_IMAGE){
			revisionThumbImageView(context, imgView);
		}

		if(nId > 0){
			imgView.setId(nId);
		}
		
		if(m_parentClickListener != null){
			imgView.setOnClickListener(m_parentClickListener);
		}
		//imgView.setTag(this);
		
		return imgView;
	}
	
	
	/**
	 * 좌측 썸네일 이미지의 변경이 필요한 경우 처리한다.
	 */
	protected void revisionThumbImageView(Context context, ImageView imageView){
		if(imageView != null){
			int nProfileImgSize = Utility.DPFromPixel(context, 72);
			imageView.setLayoutParams( new LinearLayout.LayoutParams(nProfileImgSize, nProfileImgSize));
		}
	}
	
	/**
	 * 공통된 TextView를 생성하기 위한 함수
	 */
	public TextView createTextView(Context context, int nId){
		TextView textView = new TextView(context);
		if(nId > 0){
			textView.setId(nId);
		}
		
		if(m_parentClickListener != null){
			textView.setOnClickListener(m_parentClickListener);
		}
		
		textView.setTag(this);
		textView.setTextColor(Color.parseColor("#000000"));
		textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14.5f);
		//textView.setLineSpacing(7, 1);
		return textView;
	}


	/**
	 * 서버 데이터 중 item에 저장된 데이터로 Layout을 채울때 사용함
	 * @param context - Context 레퍼런스
	 * @param parser - 서버의 데이터가 저장된 파서
	 */
	public void fillServerData(Context context, XmlManager parser){
		// 파서에 index가 -1로 들어가게 되면 해당 item에 존재하는 데이터를 가져오게 된다 
		fillServerData(context, parser, -1);
	}

	
	/**
	 * 서버 데이터 중 itemArray의 특정 Index 데이터로  Layout을 채울때 사용함
	 * @param context - Context 레퍼런스
	 * @param parser - 서버의 데이터가 저장된 파서
	 * @param nPosition - itemArray의 몇번째 데이터를 사용할지 여부
	 */
	public void fillServerData(Context context, XmlManager parser, int nPosition){
		
		String szThumbUrl = XmlParserCore.getXmlItemElementData(parser, nPosition, "image_small");
		String szTitle = XmlParserCore.getXmlItemElementData(parser, nPosition, "title");

		ImageView thubImage = (ImageView)findViewById(CTRL_ID_THUMB_IMAGE);
		if(thubImage != null && Utility.IsEmpty(szThumbUrl) == false && m_cacheManager != null){
			m_cacheManager.attachThumbailImage(szThumbUrl, (ImageView)m_leftView);
		}
		
		TextView textTitle = (TextView)findViewById(CTRL_ID_TITLE_TEXT);
		if(textTitle != null && Utility.IsEmpty(szTitle) == false){
			textTitle.setText(szTitle);
		}
	}
	
	// ##############################################
	// ## Private 함수
	// ##############################################	
	/**
	 * 부모 ViewGroup에 자식을 추가
	 */
	private void addView(ViewGroup parent, View child) {
		if(child != null){
			parent.addView(child);
		}
	}	
	
	
	public static int getLayoutId(View v){
		//ViewParent parent = view.getParent();
		
		BaseItemLayout layout = null;
		
		//FIXME : instance of 되도록 사용하지 말아야한다.. 추후 수정하자
		
		// 클릭된 child view로  부모 ListItemCellLayout를 받아온다. 
		ViewParent parent = v.getParent();
		if(parent != null){
			if(parent instanceof BaseItemLayout){
				layout = (BaseItemLayout)v.getParent();
				return layout.m_nUniqueIdx; 
			}
			
				
			parent = v.getParent().getParent();
			if(parent != null){
				if(parent instanceof BaseItemLayout){
					layout = (BaseItemLayout)parent;
					return layout.m_nUniqueIdx; 
				}

				parent = v.getParent().getParent().getParent();
				if(parent != null){
					if(parent instanceof BaseItemLayout){
						layout = (BaseItemLayout)parent;
						return layout.m_nUniqueIdx; 
					}
				}
			}
		}
		
		if(v.getParent() instanceof BaseItemLayout){
			layout = (BaseItemLayout)v.getParent();
			return layout.m_nUniqueIdx; 
		}
		else if(v.getParent().getParent() instanceof BaseItemLayout){
			layout = (BaseItemLayout)v.getParent().getParent();
			return layout.m_nUniqueIdx;
		}
		else if(v.getParent().getParent().getParent() instanceof BaseItemLayout){
			layout = (BaseItemLayout)v.getParent().getParent().getParent();
			return layout.m_nUniqueIdx;
		}
		else if(v.getParent().getParent().getParent().getParent() instanceof BaseItemLayout){
			layout = (BaseItemLayout)v.getParent().getParent().getParent().getParent();
			return layout.m_nUniqueIdx;
		}
		else if(v.getParent().getParent().getParent().getParent().getParent() instanceof BaseItemLayout){
			layout = (BaseItemLayout)v.getParent().getParent().getParent().getParent().getParent();
			return layout.m_nUniqueIdx;
		}
		else if(v.getParent().getParent().getParent().getParent().getParent().getParent() instanceof BaseItemLayout){
			layout = (BaseItemLayout)v.getParent().getParent().getParent().getParent().getParent().getParent();
			return layout.m_nUniqueIdx;
		}
		
		return -1;
	}
	
}
